package top.igotcha.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import top.igotcha.educenter.entity.UcenterMember;
import top.igotcha.educenter.entity.vo.RegisterVo;
import top.igotcha.educenter.mapper.UcenterMemberMapper;
import top.igotcha.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.igotcha.commonutils.exception.GuliException;
import top.igotcha.commonutils.utils.JwtUtils;
import top.igotcha.commonutils.utils.MD5;
import top.igotcha.commonutils.utils.ValidateUtils;

import java.util.Objects;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Gotcha
 * @since 2021-08-10
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"登录失败");
        }
        if(!ValidateUtils.isMobile(mobile)){
            throw new GuliException(20001,"手机号码格式有误");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        if (mobileMember == null){
            throw new GuliException(20001,"无此账号");
        }
        if (!Objects.equals(MD5.encrypt(password ),mobileMember.getPassword())){
            throw new GuliException(20001,"密码错误");
        }
        if (mobileMember.getIsDisabled()){
            throw new GuliException(20001,"用户被禁用");
        }
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo vo) {
        String code = vo.getCode();
        String nickName = vo.getNickname();
        String mobile = vo.getMobile();
        String password = vo.getPassword();
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(nickName) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) ){
            throw new GuliException(20001,"注册信息有误");
        }
        if(!ValidateUtils.isMobile(mobile)){
            throw new GuliException(20001,"手机号码格式有误");
        }
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!Objects.equals(code,redisCode)){
            throw new GuliException(20001,"验证码有误");
        }

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count>0){
            throw new GuliException(20001,"手机号码已存在");
        }
        UcenterMember ucenterMember = new UcenterMember();
        vo.setPassword(MD5.encrypt(password));
        BeanUtils.copyProperties(vo,ucenterMember);
        ucenterMember.setIsDeleted(false);
        ucenterMember.setAvatar("https://edu-teacherservice.oss-cn-beijing.aliyuncs.com/2021/08/07/20fd12a0b985466485a5916adf3944ce6b8f81f19bd491f9e32ff2fdf59127b.jpg");
        baseMapper.insert(ucenterMember);
    }

    @Override
    public UcenterMember getOpenIdMember(String openid) {
        UcenterMember member = baseMapper.selectOne(new QueryWrapper<UcenterMember>().eq("openid", openid));
        return member;
    }

    @Override
    public Integer countRegisterDay(String day) {
        Integer count = baseMapper.countRegisterDay(day);
        return count;
    }
}
